package com.tools.scanner.ui.dialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tools.scanner.R
import com.tools.scanner.databinding.DialogTranslateBinding
import com.tools.scanner.databinding.RecyclerTranslatorTextBinding
import com.tools.scanner.ui.ocr.OcrPreviewActivity
import com.tools.scanner.utils.ResourceUtil

class TranslateDialog(
    private val mContext: Context,
    private val mSelectCode: String,
    private val isNeedAuto: Boolean,
    private val mCallback: (name: String, code: String) -> Unit
) : AlertDialog(mContext) {

    private lateinit var mBinding: DialogTranslateBinding
    private lateinit var mAdapter: TranslatorDialogAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DialogTranslateBinding.inflate(layoutInflater)
        setContentView(mBinding.root)

        val optionList = getTranslateLanguageList(mContext)
        mAdapter = TranslatorDialogAdapter(optionList) {
            dismiss()
            mCallback(it.name, it.code)
        }

        mBinding.recyclerView.layoutManager = LinearLayoutManager(context)
        mBinding.recyclerView.adapter = mAdapter

        mBinding.btnCancel.setOnClickListener {
            dismiss()
        }

        mBinding.btnConfirm.setOnClickListener {
            dismiss()
        }
    }

    override fun show() {
        super.show()
        window?.let {
            it.setGravity(Gravity.BOTTOM)
            val layoutParams = it.attributes
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            it.attributes = layoutParams
            it.setBackgroundDrawableResource(android.R.color.transparent)
        }
    }

    private fun getTranslateLanguageList(
        context: Context
    ): List<TranslateInfo> {
        val resultList = mutableListOf<TranslateInfo>()

        var selectIndex = -1
        OcrPreviewActivity.LAN_CODE_MAP.forEachIndexed { index, code ->
            val isSelect = mSelectCode == code
            if (isSelect) {
                selectIndex = index
            }
            val resourceName = "translate_${code}"
            val name = ResourceUtil.getString(context, resourceName)
            resultList.add(TranslateInfo(name, code, isSelect))
        }

        if (!isNeedAuto) {
            resultList.removeAt(0)

            if (selectIndex != -1) {
                selectIndex--
                resultList.removeAt(selectIndex).let {
                    resultList.add(0, it)
                }
            }
        }

        if (selectIndex != 0 && selectIndex != -1) {
            resultList.removeAt(selectIndex).let {
                resultList.add(1, it)
            }
        }

        return resultList
    }

}

private class TranslatorDialogAdapter(
    private val mList: List<TranslateInfo>,
    private val callback: (TranslateInfo) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding = RecyclerTranslatorTextBinding.inflate(layoutInflater, parent, false)
        return TranslatorDialogViewHolder(binding, callback)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is TranslatorDialogViewHolder) {
            holder.refresh(mList[position])
        }
    }

    private class TranslatorDialogViewHolder(
        private val mBinding: RecyclerTranslatorTextBinding,
        private val callback: (TranslateInfo) -> Unit
    ) : RecyclerView.ViewHolder(mBinding.root) {

        init {
            mBinding.root.setOnClickListener {
                val info = it.tag
                if (info is TranslateInfo) {
                    callback(info)
                }
            }
        }

        fun refresh(info: TranslateInfo) {
            mBinding.root.tag = info
            mBinding.text.text = info.name

            val colorId = if (info.isSelect) {
                R.color.color_d2e5f4
            } else {
                R.color.color_ffffff
            }
            mBinding.root.setBackgroundResource(colorId)
        }

    }

}

data class TranslateInfo(val name: String, val code: String, var isSelect: Boolean = false)